package m3d28;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

//存储自定义类型
//key必须重写
public class eHashMapPerson {
    public static void main(String[] args) {
        HashMap<String,dPerson> hashMap = new HashMap();
        hashMap.put("a",new dPerson("aaa",1));
        hashMap.put("b",new dPerson("aaa",2));
        hashMap.put("c",new dPerson("aaa",3));
        //String类key重复，替换
        hashMap.put("a",new dPerson("aaa",4));

        Set<String> set = hashMap.keySet();
        for (String key:set
             ) {
            System.out.println(hashMap.get(key));
        }
        System.out.println("===================");

        //person作为key时必须重写hashcose和eq方法
        HashMap<dPerson,String> hashMap1= new HashMap<>();
        hashMap1.put(new dPerson("aaa",1),"a");
        hashMap1.put(new dPerson("aaa",2),"a");
        hashMap1.put(new dPerson("aaa",3),"a");
        hashMap1.put(new dPerson("aaa",1),"d");
        Set<Map.Entry<dPerson,String>> set1 = hashMap1.entrySet();
        for (Map.Entry<dPerson,String> entry:set1
             ) {
            dPerson key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+value);
        }

    }
}
